package com.kele.base.response;

public class Result<T> {
    // 状态码
    private int code;
    // 错误信息
    private String msg;
    // 返回结果
    private T data;

    public static <T>Result<T> ok(T result){
        return new Result<T>().success("成功",result);
    }

    public static <T>Result<T> ok(T result, String message){
        return new Result<T>().success(message, result);
    }


    public static <T>Result<T> ok(String message){
        return new Result<T>().success(message);
    }

    public static <Object>Result<Object> ok(){
        return new Result<Object>().success();
    }

    public static <Object>Result<Object> error(String message){
        return  new Result<Object>().failed(HttpStatus.INTERNAL_SERVER_ERROR, message);
    }

    public static Result<Object> error(HttpStatus errorCode, String message){
        return  new Result<Object>().failed(errorCode, message);
    }

    public static Result<Object> error(int errorCode, String message){
        return new Result<Object>().failed(errorCode, message);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }


    private Result<T> success(String message, T object) {
        this.code = HttpStatus.OK.getStatusCode();
        this.msg = message;
        this.data = object;
        return this;
    }

    private Result<T> success(String message) {
        this.code = HttpStatus.OK.getStatusCode();
        this.msg = message;
        return this;
    }

    private Result<T> success() {
        this.code = HttpStatus.OK.getStatusCode();
        return this;
    }


    private Result<T> failed(HttpStatus status, String message) {
        return failed(status.getStatusCode(), message);
    }

    private Result<T> failed(int errorCode, String message){
        this.code = errorCode;
        this.msg = message;
        return this;
    }
}